import _ from "lodash";
import { defineComponent, ref, watch, computed } from "vue";
const name = "ep-render-form";
const defaultProps = {
  /** 表单列表 */
  widgetList: {
    type: Array,
    default: null
  },
  /** 表单数据 */
  formData: {
    type: Object,
    default: null
  },
  /**
   * 组件权限 0可编辑 1只读 2隐藏
   * key value
   */
  permissions: {
    type: Object,
    default: null
  },
  /** 表单设置 */
  formConfig: {
    type: Object,
    default: () => ({
      labelPosition: "left",
      labelWidth: 80,
      size: "default"
    })
  }
};
const _sfc_main = defineComponent({
  name,
  props: defaultProps,
  emits: ["change"],
  setup(props, { emit }) {
    const widgetFormRef = ref(null);
    const formModel = ref({});
    watch(
      () => props.formData,
      (val) => {
        if (val) {
          formModel.value = _.cloneDeep(val);
        }
      },
      { immediate: true }
    );
    const _filterWidget = (list, permissions) => {
      const widgetList = [];
      if (list && Array.isArray(list)) {
        for (let index = 0; index < list.length; index++) {
          const item = _.cloneDeep(list[index]);
          if (item.type === "card" && item.components) {
            widgetList.push(..._filterWidget(item.components, permissions));
          } else {
            item.options.permission = 0;
            if (permissions && permissions[item.id] !== void 0) {
              item.options.permission = permissions[item.id];
            }
            widgetList.push(item);
          }
        }
      }
      return widgetList;
    };
    const widgetListFilter = computed(() => {
      const list = _filterWidget(props.widgetList, props.permissions);
      return list;
    });
    const labelPosition = computed(() => {
      var _a;
      const position = ((_a = props.formConfig) == null ? void 0 : _a.labelPosition) || "left";
      return position;
    });
    const labelWidth = computed(() => {
      var _a, _b;
      if (((_a = props.formConfig) == null ? void 0 : _a.labelPosition) === "top") {
        return "100%";
      }
      return (_b = props.formConfig) == null ? void 0 : _b.labelWidth;
    });
    const size = computed(() => {
      var _a;
      return ((_a = props.formConfig) == null ? void 0 : _a.size) || "default";
    });
    return { widgetFormRef, formModel, widgetListFilter, labelPosition, labelWidth, size };
  }
});
export {
  _sfc_main as default
};
